package com.jack.thinking.thread.core._3;

import java.util.ArrayList;
import java.util.List;

/**
 * Thread's status
 * every lock object monitor have two sequence, one is ready queue , the another one is blocking queue
 * ready queue have the thread what has already get lock.
 * block queue have the thread what has blocked lock.
 * 
 * 
 * @author wangbingwu
 *
 */
public class NotifyListSizeOver {
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<>();
		ConsumeClass c = new ConsumeClass(list);
		ProduceClass p = new ProduceClass(list);
		c.start();
		p.start();
	}
}

class ProduceClass extends Thread{
	private List<Integer> source;
	
	
	public ProduceClass(List<Integer> source) {
		super();
		this.source = source;
	}

	@Override
	public void run() {
		synchronized (source) {
			System.out.println("produce start cycle");
			for (int i = 0; i < 10; i++) {
				source.add(i);
				try {
					Thread.sleep(200L);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(source.size() == 5) {
					source.notify();
					break;
				}
			}
			System.out.println("produce end cycle");
		}
	}
}

class ConsumeClass extends Thread{
	private List<Integer> source;
	
	
	public ConsumeClass(List<Integer> source) {
		super();
		this.source = source;
	}

	@Override
	public void run() {
		synchronized (source) {
			try {
				System.out.println("consume start cycle");
				source.wait();
				System.out.println("consume cycle ...");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("consume end cycle");
		}
	}
}